package org.zephyr.fastboot.example.Realm;

import lombok.val;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.zephyr.fastboot.example.service.TestService;
import org.zephyr.fastboot.example.service.impl.TestServiceImpl;

/**
 * @Author: ZephyrGao
 * @Date: 2024/7/19 15:55
 * @Description:
 */
public class MyRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        System.out.println("执行了鉴权");
        return null;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)  {
        System.out.println("执行了认证");
        String loginName = (String) authenticationToken.getPrincipal();
        // 这里应该通过loginName查询数据库，获取用户信息
        TestService service = new TestServiceImpl();
        String password = service.findUserNameByUserName(loginName);
        if ("".equals(password)) {
            throw new UnknownAccountException("用户名不存在");
        }

        return new SimpleAuthenticationInfo(loginName, password, getName());
    }
}
